from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.views import View

from forum.models import forum
from cars.models import CarEvaluate
from reservation.models import Reservation
from users.models import User
from order.models import Order

# Create your views here.


#讨论列表
class ForumView(View):
    def get(self, request):
        #获取当前用户下的预约数量
        user = request.session.get('user')
        if user:
            reservation_list = Reservation.objects.filter(user_id=user.id)
            number = len(reservation_list)
            #获取所有的讨论内容
            writing_list= forum.objects.all()
            return render(request, 'templates/forum/forum.html', {'writing_list':writing_list, 'number':number})
        else:
            writing_list = forum.objects.all()
            return render(request, 'templates/forum/forum.html', {'writing_list': writing_list})

 #去评价
class EditForumView(View):
    def get(self, request,car_id):
        user = request.session.get('user')
        reservation_list = Order.objects.filter(user_id=user.id)
        number = len(reservation_list)
        return render(request, 'templates/forum/edit_form.html', {'car_id':car_id, 'number':number})


# 添加商品评论
class EditOverView(View):
    def post(self, request,car_id):
        #获取用户
        user = request.session.get('user')
        # title = request.POST.get('title')
        content = request.POST.get('content')
        users = User.objects.get(id=user.id)
        #插入数据
        CarEvaluate.objects.create(ename=user.name,head=users.head,evaluate=content,Cars_id=car_id,user_id=user.id)
        return HttpResponseRedirect('/forum/')


#添加用户讨论
class AddcontentView(View):
    def get(self, request):
        user = request.session.get('user')
        reservation_list = Reservation.objects.filter(user_id=user.id)
        number = len(reservation_list)
        writing_list = forum.objects.all()
        return render(request, 'templates/forum/forum.html', {'writing_list':writing_list, 'number':number})

    def post(self, request):
        user = request.session.get('user')
        content = request.POST.get('comment')
        users = User.objects.get(id=user.id)
        forum.objects.create(name=user.name,head=users.head,content=content,user_id=user.id)
        return HttpResponseRedirect('/forum/')

#删除评论
def DeletedcontentView(request,c_id):
    content = CarEvaluate.objects.get(id=c_id)
    content.delete()
    return HttpResponseRedirect('/evaluate/')